package com.log320.huffman;

import java.util.ArrayList;
/**
 * Utility class containing static methods for the conversion of huffman binary code
 * to their canonical version.
 *
 */
public class CannonicalConverter {
	
	/**
	 * Static method used to convert the binary code of an array
	 * to its canonical version. It receive the ArrayList as a parameter
	 * and modify it by references. 
	 * @param preSortedCodeList  The list to convert
	 */
	public static void toCannonical(ArrayList<BinaryCode> preSortedCodeList){
		int value =0;
		int previousBitLength=1;
		int actualBitLength=0;
		for (BinaryCode bin: preSortedCodeList){
			if (bin.size() != 0){
				actualBitLength = bin.size();
				value = (value) << (actualBitLength - previousBitLength);
				bin.setToBit(value);
				previousBitLength=actualBitLength;
				value++;
			}
		}
	}

}
